package com.rasenhund.android.grecorder.activity;

import java.io.IOException;
import java.io.InputStream;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.rasenhund.android.grecorder.R;
import com.rasenhund.android.grecorder.dao.RecordDao;
import com.rasenhund.android.grecorder.helper.DbHelper;
import com.rasenhund.android.grecorder.schema.CategoryCount;

/**
 * 出撃傾向グラフ画面
 */
public class TrendActivity extends BaseActivity {
    // @SuppressWarnings("unused")
    // private static final String TAG = TrendActivity.class.getSimpleName();
    private final TrendActivity self = this;

    CategoryCount mCategoryCount20 = null;
    // CategoryCount mCategoryCount50 = null;
    CategoryCount mCategoryCountAll = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.trend);

        initView();
        
    }

    /**
     * Viewの初期化
     */
    private void initView() {

        // 過去20戦、通算の出撃率をレーダーチャートで表示する

        // WebViewの初期設定
        WebView webView = (WebView) findViewById(R.id.trend_webview);
        webView.setWebViewClient(new WebViewClient() {
        });

        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setSaveFormData(false);
        settings.setSavePassword(false);
        settings.setSupportZoom(true);
        settings.setDefaultTextEncodingName("utf-8");

        // テンプレートHtmlの取得
        String sHtml = getTemplateHtml("graph.html");

        // 表示用のデータ取得
        this.getData();

        // テンプレートに置換する文字列を作成
        // @formatter:off
        String sItem20 = "\"過去20戦\"" + 
                    ", " + mCategoryCount20.getGrappPer(100) + 
                    ", " + mCategoryCount20.getProxPer(100) + 
                    ", " + mCategoryCount20.getShootPer(100) + 
                    ", " + mCategoryCount20.getCannonPer(100) + 
                    ", " + mCategoryCount20.getSnipePer(100);
        
//        String sItem50 = "\"過去50戦\"" + 
//                    ", " + mCategoryCount50.getGrappPer(100) + 
//                    ", " + mCategoryCount50.getProxPer(100) + 
//                    ", " + mCategoryCount50.getShootPer(100) + 
//                    ", " + mCategoryCount50.getCannonPer(100) + 
//                    ", " + mCategoryCount50.getSnipePer(100);
        		       
        String sItemAll = "\"通算\"" + 
                    ", " + mCategoryCountAll.getGrappPer(100) + 
                    ", " + mCategoryCountAll.getProxPer(100) + 
                    ", " + mCategoryCountAll.getShootPer(100) + 
                    ", " + mCategoryCountAll.getCannonPer(100) + 
                    ", " + mCategoryCountAll.getSnipePer(100);
        // @formatter:on
        
        // テンプレートにデータ埋め込み
        sHtml = sHtml.replace("ITEMS_20", sItem20);
        sHtml = sHtml.replace("ITEMS_ALL", sItemAll);

        webView.loadDataWithBaseURL("file:///android_asset/", sHtml, "text/html", "utf-8", null);

    }

    /**
     * htmlファイルの中身をバイナリデータとして取得
     * 
     * @param sPath
     * @return
     */
    private String getTemplateHtml(String sPath) {
        // TODO: よく使うなら共通化しても良いかも

        InputStream in = null;
        byte[] buff = null;
        String sRet = "";

        try {

            in = getAssets().open(sPath);
            buff = new byte[in.available()];

            in.read(buff);

            sRet = new String(buff);

        } catch (Exception e) {

            // Log.e(TAG, e.toString());

        } finally {

            try {
                in.close();
            } catch (IOException e) {
                // Log.e(TAG, e.toString());
            }

        }

        return sRet;
    }

    /**
     * 表示用データの取得
     */
    private void getData() {

        DbHelper dbHelper = new DbHelper(self);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        RecordDao dao = new RecordDao(db);

        int iArmyCd = getArmyCode();
        mCategoryCount20 = dao.getCategoryCount(iArmyCd, 20);
        mCategoryCountAll = dao.getCategoryCount(iArmyCd);

        db.close();

    }

}
